<template>
  <div class="number-login">
    <el-form
      ref="loginFormRef"
      :model="userMessage"
      :rules="rules"
      size="large"
    >
      <el-form-item label="账号" prop="name">
        <el-input v-model="userMessage.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="userMessage.password"
          show-password
        ></el-input>
      </el-form-item>
      <div class="login-control">
        <el-checkbox v-model="isSavePassword" label="记住密码" size="small" />
        <el-link type="info">忘记密码</el-link>
      </div>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue";
import { useStore } from "vuex";
import { ElForm } from "element-plus";

import { rules } from "../config/number-login";
import localCache from "@/utils/localCache";

export default defineComponent({
  setup() {
    const store = useStore();
    const userMessage = reactive({
      name: localCache.getCache("name") ?? "",
      password: localCache.getCache("password") ?? ""
    });
    const isSavePassword = ref(true);
    // 登录
    const loginFormRef = ref<InstanceType<typeof ElForm>>();
    const numberLoginMethod = () => {
      loginFormRef.value?.validate((valid) => {
        if (!valid) return;
        // 记住密码
        if (isSavePassword.value) {
          localCache.setCache("name", userMessage.name);
          localCache.setCache("password", userMessage.password);
        } else {
          localCache.deleteCache("name");
          localCache.deleteCache("password");
        }
        store.dispatch("login/numberLoginAction", { ...userMessage });
      });
    };

    return {
      userMessage,
      rules,
      isSavePassword,
      numberLoginMethod,
      loginFormRef
    };
  }
});
</script>

<style scoped lang="less">
.number-login {
  padding: 20px 30px 0 30px;
  .login-control {
    // padding: 0 30px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
}
.el-link {
  font-size: 12px !important;
}
</style>
